/**
 * 正则表达式工具类
 */
/* eslint-disable quotes */
export function isMail(str) {
  return /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/.test(str)
}

export function isTel(str) {
  return /^([0-9]{3,4}-)?[0-9]{7,8}$/.test(str)
}

export function isMobile(str) {
  return /^((\+?86)|(\(\+86\)))?(13[012356789][0-9]{8}|15[012356789][0-9]{8}|18[02356789][0-9]{8}|147[0-9]{8}|1349[0-9]{7})$/.test(
    str
  )
}

export function isVerCode(str) {
  return /^[a-zA-Z0-9]{5}$/.test(str)
}

export function isPostCode(str) {
  return /^\d{6}$/.test(str)
}

export function isUserName(str) {
  return /^([a-zA-Z0-9_]){3,50}$/.test(str)
}

export function isPhone(str) {
  return isTel(str) || isMobile(str)
}

export function isEmail(str) {
  return /^([a-zA-Z]|[0-9])(\w|-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/.test(str)
}

export function isPassword(str) {
  return (
    /[a-z]/.test(str) &&
    /[A-Z]/.test(str) &&
    /[0-9]/.test(str) &&
    /[`\-~!@#$%^&*()_+<>?:"{},./;'[\]]/.test(str)
  )
}

export function isIp(str) {
  return /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/.test(
    str
  )
}

export function isDomain(str) {
  return /^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/.test(
    str
  )
}

export function isUrl(str) {
  const regex =
    '^((https|http|ftp|rtsp|mms)?://)' +
    "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" + // ftp的user@
    '(([0-9]{1,3}.){3}[0-9]{1,3}' + // IP形式的URL- 199.194.52.184
    '|' + // 允许IP和DOMAIN（域名）
    "([0-9a-z_!~*'()-]+.)*" + // 域名- www.
    '([0-9a-z][0-9a-z-]{0,61})?[0-9a-z].' + // 二级域名
    '[a-z]{2,6})' + // first level domain- .com or .museum
    '(:[0-9]{1,4})?' + // 端口- :80
    '((/?)|' + // a slash isn't required if there is no file name
    "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$"
  return new RegExp(regex).test(str)
}
